package Util::Logger;

use strict;
use POSIX;

# ------------------ Config -----------------

my $fmt = '%Y/%m/%d  %H:%M:%S';
my $nl  = "\r\n";

# --------------- End of Config -------------

sub new($) {

	my ( $class, $fname ) = @_;
	
	open my $F, ">> $fname" 	or return undef;

	my $this = {
		FNAME		=> $fname,
		F		=> $F,
	};
	bless( $this, $class );
	$this;
}

sub write_log {
	my ( $this, $level, $msg ) = @_;
	chomp $msg;
	
	my $s_time = strftime '%Y/%m/%d  %H:%M:%S', localtime();
	
	syswrite $this->{F}, sprintf( "%s  { %5d }  [%5s]  %s".$nl, $s_time, $$, $level, $msg );
}

sub debug {
	my ( $self, $msg ) = @_;
	write_log( $self, 'DEBUG', $msg ); 
}

sub info {
	my ( $self, $msg ) = @_;
	write_log( $self, 'INFO', $msg ); 
}

sub warning {
	my ( $self, $msg ) = @_;
	write_log( $self, 'WARNING', $msg ); 
}

sub error {
	my ( $self, $msg ) = @_;
	write_log( $self, 'ERROR', $msg ); 
}

sub alert {
	my ( $self, $msg ) = @_;
	chomp $msg;
	syswrite STDERR, $msg.$nl;
	write_log( $self, 'ALERT', $msg ); 
}

sub DESTROY {
	my $self = @_;
	my $f=$self->{F};
	close $f;
}

1;
